﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

namespace Dimok.Data.Database {

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Keys filter. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public struct KeysFilter {
		public string Filter;
		public object[] Params;
		public string KeysList;

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	excludeEmptyValues = false. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="keys">	. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public KeysFilter(IDictionary keys) : this(keys, false) { }

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	excludeEmptyValues = false. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="keys">								. </param>
		/// <param name="excludeEmptyValues">	true to exclude empty values. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public KeysFilter(IDictionary keys, bool excludeEmptyValues) {
			List<string> lstWhere = new List<string>();
			ArrayList alParams = new ArrayList();
			foreach (DictionaryEntry key in keys) {
				if (excludeEmptyValues && (key.Value + "") == "") continue;
				string filter = key.Value != DBNull.Value ? key.Key + "=@" + key.Key : key.Key + " IS NULL";
				lstWhere.Add(filter);
				if (key.Value != DBNull.Value) alParams.Add(key.Value);
			}
			Filter = string.Join(" AND ", lstWhere.ToArray());
			Params = alParams.ToArray();
			KeysList = OrderedDictionaryEx.KeyList(keys, true);
		}
	}
}
